package org.example.member.controller.front;

import org.example.core.utils.R;
import org.example.member.entity.MemberReceiveAddressEntity;
import org.example.member.service.MemberReceiveAddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;



/**
 * @author xiaojie_cc
 * @version 1.0
 */
@RestController
@RequestMapping("/ums/front/address")
public class MemberReceiveAddressFrontController {
    @Autowired
    private MemberReceiveAddressService memberReceiveAddressService;

    // 获取对应会员的收货地址
    @GetMapping("/list")
    private R<?> address(){
        List<MemberReceiveAddressEntity> memberReceiveAddress = memberReceiveAddressService.queryAddressByMemberId();
        return R.ok(memberReceiveAddress);
    }

    // 根据地址id回显某一个地址数据
    @GetMapping("/{addressId}")
    public R<MemberReceiveAddressEntity> addressInfo(@PathVariable("addressId") Long addressId){
        MemberReceiveAddressEntity memberReceiveAddress = memberReceiveAddressService.queryAddressInfoById(addressId);
        return R.ok(memberReceiveAddress);
    }

    // 修改地址信息
    @PutMapping("/update/{id}")
    public R<?> update(@PathVariable("id") Long id, @RequestBody MemberReceiveAddressEntity memberReceiveAddress){
        return memberReceiveAddressService.updateMemberAddress(id,memberReceiveAddress)>0? R.ok():R.error();
    }

    // 新增地址
    @PostMapping("/add")
    public R<?> add(@RequestBody MemberReceiveAddressEntity memberReceiveAddress){
        return memberReceiveAddressService.addMemberAddress(memberReceiveAddress)>0? R.ok():R.error();
    }

    @DeleteMapping("/delete/{id}")
    public R<?> delete(@PathVariable("id") Long id){
        return memberReceiveAddressService.deleteAddressById(id)>0? R.ok():R.error();
    }
}
